//
//  HMPhotoCell.swift
//  图片浏览器
//
//  Created by Apple on 16/7/31.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

protocol HMPhotoCellDelegate: NSObjectProtocol {
    // 加号按钮点击
    func photoCellDidClickAddButton(cell: HMPhotoCell)
    
    // 删除按钮点击
    func photoCellDidClickDeleteButton(cell: HMPhotoCell)
}

class HMPhotoCell: UICollectionViewCell {
    
    /// 设置加号按钮
    func setupAddButton() {
        self.addButton.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        
        // 删除按钮隐藏掉
        self.deleteButton.hidden = true
    }
    
    /// cell要显示的图片
    var image: UIImage? {
        didSet {
            // 将图片显示到加号按钮上面
            addButton.setImage(image, forState: UIControlState.Normal)
            
            // 显示删除按钮
            deleteButton.hidden = false
        }
    }
    
    weak var delegate: HMPhotoCellDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    private func setupUI() {
        self.contentView.addSubview(addButton)
        self.contentView.addSubview(deleteButton)
        
        // 约束
        addButton.translatesAutoresizingMaskIntoConstraints = false
        deleteButton.translatesAutoresizingMaskIntoConstraints = false
        
        let views = ["ab" : addButton, "db": deleteButton]
        // 加号按钮
        self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[ab]-0-|", options: [], metrics: nil, views: views))
        self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[ab]-0-|", options: [], metrics: nil, views: views))
        
        // 删除按钮
        // 右边和父控件右边重合
        self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[db]-0-|", options: [], metrics: nil, views: views))
        
        // 顶部和父控件顶部重合
        self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[db]", options: [], metrics: nil, views: views))
    }
    
    // MARK: - 点击事件
    @objc private func addButtonClick() {
        delegate?.photoCellDidClickAddButton(self)
    }
    
    @objc private func deleteButtonClick() {
        delegate?.photoCellDidClickDeleteButton(self)
    }
    
    // MARK: - 懒加载
    /// 加号按钮
    private lazy var addButton: UIButton = {
        let button = UIButton()
       
        // 设置
//        button.setBackgroundImage(<#T##image: UIImage?##UIImage?#>, forState: <#T##UIControlState#>)
        button.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        button.addTarget(self, action: "addButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
       
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
